
$(function () {
    var background = chrome.extension.getBackgroundPage();
    chrome.windows.getCurrent(function (w) {
        chrome.tabs.query({
            active: true,
            windowId: w.id
        }, function (tabs) {
            if (tabs.length > 0) {
                $('body').width(350).height(250);
                var _t = tabs[0];
                var keyid = background.Site.url2key(null, _t.url);
                if (!keyid) { return; };
                var confKey = keyid.key;
                var id = keyid.id;
                $('div[c=rank]').stars({
                    inputType: "select",
                    split: 1
                }).show();

                if (confKey && background.exports.work.checkUrl(confKey, id)) {
                    //已有数据 画图
                    drawChart(background.exports.work.getPrices(confKey, id), 'chart_div');
                    var rank = background.exports.work.getRank(confKey, id);
                    var starWrapper = $('div[c=rank]');
                    console.log(rank);

                    setRank(rank, starWrapper);
                    //                            background.Site.getPageInfo({
                    //                                confKey: confKey,
                    //                                id: id,
                    //                                url: _t.url,
                    //                                success: function (_info) {
                    //                                    //更新本url相关的数据信息
                    //                                    _info.rank = rank;
                    //                                    console.log(JSON.stringify(_info));
                    //                                    background.Site.handle.updateSiteData(confKey, _info);
                    //                                }
                    //                            });
                    $('#action_div').append(
                                $('<button>').text('删除关注').attr('class', 'btn btn-primary').click(function () {
                                    $(this).text('处理中').attr('disabled', 'true');
                                    background.exports.work.delUrl(confKey, id);
                                    location.reload(true);
                                })
                            );
                } else {

                    //stars
                    $('div[c=rank]').stars('select', 4).stars({
                        //captionEl: $(this).prev('div').find('span[c=rankvalue]')
                        captionEl: $('#captionEl')
                    });

                    //没有关注 显示添加关注选项
                    $('#action_div').append(
                                $('<button>').text('加关注').attr('class', 'btn btn-primary').click(function () {
                                    $(this).text('处理中').attr('disabled', 'true');
                                    background.exports.work.addUrl(confKey, id);
                                    background.Site.getPageInfo({
                                        confKey: confKey,
                                        id: id,
                                        url: _t.url,
                                        success: function (_info) {
                                            //更新本url相关的数据信息
                                            _info.rank = getStarValue('rankStar');

                                            background.Site.handle.updateSiteData(null, _info);
                                            location.reload(true);
                                        }
                                    }, confKey);
                                })
                            );
                }
            }
        });
    });

    function drawChart(data, divId) {
        var xAxis = [], yAxis = [], time;
        for (var i = 0; i < data.length; i++) {
            if (data[i][1] > -1) {//去掉无报价的项
                time = new Date(data[i][0]);
                xAxis.push(time.getDate() + '/' + (time.getMonth() + 1));
                yAxis.push(data[i][1] - 0);
            }
        }
        return new Highcharts.Chart({
            chart: {
                renderTo: divId,
                defaultSeriesType: 'line',
                height: 200,
                width: 300,
                spacingTop: 20,
                marginRight: 80,
                marginBottom: 25
            },
            title: {
                text: '历史价格',
                x: -20 //center
            }, /*
                    subtitle: {
                        text: 'Source: WorldClimate.com',
                        x: -20
                    },*/
            xAxis: {
                categories: xAxis
            },
            yAxis: {
                title: {
                    text: '价格'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                            this.x + ': ￥' + this.y + '';
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: [{
                name: '价格',
                data: yAxis
            }]
        });
    }

    function setRank(rank, starWraper) {
        $(starWraper).stars('select', rank).stars({
            captionEl: $('#captionEl')
        });
    }

    function getStarValue(starwrapperId) {
        var ui = $("#" + starwrapperId).data("stars");
        // Read options
        var currValue = ui.options.value; // Get current Value

        return currValue || 4;
    }

    $('#btnOptions').attr('href', chrome.extension.getURL('/options.html'));
});